package com.example.hotel_lxy.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.DialogDataReserveCbBean;
import com.example.hotel_lxy.bean.ScheduledBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.ui.activity.LoginActivity;
import com.example.hotel_lxy.ui.activity.ReserveActivity;
import com.example.hotel_lxy.utils.SPUtils;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.DisplayUtils;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;
//餐标
public class    DataReserveCbDialog extends Dialog {
    private Context mContext;
    RecyclerView rv_Data_selection;
    private static  String tid="";
    public DataReserveCbDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    private View init() {
        View view = View.inflate(mContext, R.layout.dialog_dataselection, null);
        rv_Data_selection=view.findViewById(R.id.rv_Data_selection);
        showData();
        return view;
    }

    private void showData() {
        String sched_id = SPUtils.get(mContext, "sched_id", "").toString();
        OkHttpUtils.post()
                .url(Api.POST_SCHEDULED_STANDARD)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("bid",SPUtils.get(mContext, "sched_id", "").toString())
                .addParams("tid",tid)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "餐标onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "餐标onResponse   " + response);
                        BeanBean beanBean= JsonUtils.parseByGson(response, BeanBean.class);
                        if(beanBean.getCode().equals("4")){
                            SPUtils.put(mContext,"token","");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            mContext.startActivity(intent);
                            return;
                        }
                        if(beanBean.getCode().equals("0")){
                            DialogDataReserveCbBean scheduledBean  = JsonUtils.parseByGson(response, DialogDataReserveCbBean.class);
                            if(scheduledBean.getData().getData().size()==0){
                                ToastUtils.shortToast(mContext,"无可用餐标，可手动输入");
                                dismiss();
                                return;
                            }
                            for(int i=0;i<scheduledBean.getData().getData().size();i++){
                                rv_Data_selection.setLayoutManager(new LinearLayoutManager(mContext));
                                RBaseAdapter<DialogDataReserveCbBean.DataBeanX.DataBean> rBaseAdapterAllType = new RBaseAdapter<DialogDataReserveCbBean.DataBeanX.DataBean>(
                                        mContext, R.layout.item_item_popwindow, scheduledBean.getData().getData()) {
                                    @Override
                                    protected void fillItem(RViewHolder holder, DialogDataReserveCbBean.DataBeanX.DataBean item, int position) {
                                        TextView text_title = holder.itemView.findViewById(R.id.text_title);
                                        text_title.setText(item.getName());
                                        holder.itemView.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                String string = SPUtils.get(mContext, "money", "").toString();
                                                if(Float.valueOf(string)>Float.valueOf(item.getMoney())){
                                                    ToastUtils.shortToast(mContext,"不能低于最低餐标"+string+"元");
                                                    dismiss();
                                                    return;
                                                }
                                                listener.onPositiveClick(item.getId()+"",item.getDid(),item.getName(),item.getMoney());
                                                dismiss();
                                            }
                                        });
                                    }

                                    @Override
                                    protected int getViewType(DialogDataReserveCbBean.DataBeanX.DataBean item, int position) {
                                        return 0;
                                    }
                                };
                                rv_Data_selection.setAdapter(rBaseAdapterAllType);
                            }
                        }else{
                            ToastUtils.shortToast(mContext,""+beanBean.getMsg());
                            dismiss();
                        }

                    }
                });

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
//        setCancelable(false);
        setContentView(init());
    }

    public void showDialog() {
        if (!isShowing()) {
            show();
            WindowManager.LayoutParams lp = getWindow().getAttributes();
//            lp.height = DisplayUtils.getDisplayWidth(mContext) * 5 / 5;
            lp.width = DisplayUtils.getDisplayWidth(mContext) * 8 / 10;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(lp);
            getWindow().setGravity(Gravity.CENTER);
        }
    }

    public static DataReserveCbDialog show(Context context, OnDialogClickListener listener,String tids) {
        DataReserveCbDialog dialog = new DataReserveCbDialog(context, R.style.BottomDialogStyle);
        dialog.setListener(listener);
        tid=tids;
        dialog.showDialog();
        return dialog;
    }

    public OnDialogClickListener listener;

    public DataReserveCbDialog setListener(OnDialogClickListener listener) {
        this.listener = listener;
        return this;
    }

    public interface OnDialogClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(String id,String did, String name,String d_moneys);
    }
}
